#ifndef ODB_CHAT_SESSION_INFO_HXX
#define ODB_CHAT_SESSION_INFO_HXX

#include <boost/date_time/posix_time/posix_time.hpp>
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#include <string>

#include "chat_session_member.hxx"

namespace chat {
enum class ChatSessionType {
    SINGLE = 1,
    GROUP = 2,
};

#pragma db object table("session_info")
class ChatSessionInfomation {
   public:
    ChatSessionInfomation() = default;
    ChatSessionInfomation(const std::string& chat_session_id,
                    const std::string& chat_session_name,
                    unsigned char chat_session_type)
        : chat_session_id_(chat_session_id),
          chat_session_name_(chat_session_name),
          chat_session_type_(chat_session_type) {}

    void chat_session_id(const std::string& chat_session_id) {
        chat_session_id_ = chat_session_id;
    }
    const std::string& chat_session_id() const { return chat_session_id_; }

    void chat_session_name(const std::string& chat_session_name) {
        chat_session_name_ = chat_session_name;
    }
    const std::string& chat_session_name() const { return chat_session_name_; }

    void chat_session_type(unsigned char chat_session_type) {
        chat_session_type_ = chat_session_type;
    }
    unsigned char chat_session_type() const { return chat_session_type_; }

   private:
    friend class odb::access;
#pragma db id auto
    unsigned long id_;
#pragma db type("VARCHAR(64)") index unique
    std::string chat_session_id_;  // 会话id
#pragma db type("VARCHAR(64)")
    std::string chat_session_name_;    // 会话名称
    unsigned char chat_session_type_;  // 会话类型 1-single 2-group
};
using ChatSessionInfomationPtr = std::shared_ptr<ChatSessionInfomation>;

// 传入的条件必须为：csi::chat_session_type_ == ChatSessionType::Single &&
// csm1::user_id_ == 自己的id &&        -------    保证是自己的会话
// csm2::user_id_ != csm1::user_id_    --------   除去自己的，就是对方的uid
#pragma db view object(ChatSessionInfomation = csi) \ 
                object(ChatSessionMember = csm1 : csi::chat_session_id_ == \
                                           csm1::session_id_) \ 
                object(ChatSessionMember = csm2 : csi::chat_session_id_ == \
                                           csm2::session_id_) \ 
                query((?))
struct SingleSessionInfo {
#pragma db column(csi::chat_session_id_)
    std::string chat_session_id;
#pragma db column(csm2::user_id_)
    std::string peer_id;
};

// 传入的条件必须为：csi::chat_session_type_ == ChatSessionType::Group &&
// csm::user_id_ == 自己的id
#pragma db view object(ChatSessionInfomation = csi) \ 
                object(ChatSessionMember = csm : csi::chat_session_id_ == \
                                           csm::session_id_) \ 
                query((?))
struct GroupSessionInfo {
#pragma db column(csi::chat_session_id_)
    std::string chat_session_id;
#pragma db column(csi::chat_session_name_)
    std::string chat_session_name;
};

}  // namespace chat

#endif  // ODB_CHAT_SESSION_INFO_HXX
